package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.controller.user.ShopController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DishInSetmealException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;

    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //套餐表插入套餐
        setmealMapper.insert(setmeal);
        //返回插入的套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //菜品套餐关系表插入数据
        if(setmealDishes!=null && setmealDishes.size()!=0){
            setmealDishMapper.insert(setmealDishes);
        }
    }
    /*
    * 套餐分页查询
    * */
    @Override
    public PageResult setmealPageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVO = (Page<SetmealVO>) setmealMapper.setmealPageQuery(setmealPageQueryDTO);
        return new PageResult(setmealVO.getTotal(),setmealVO.getResult());
    }

    @Override
    public void modifyStatus(int status, long id) {
        //判断是起售还是停售
        if(status == StatusConstant.ENABLE){
            //是起售，就要判断是否有菜品处于停售状态
            //先查询这个套餐关联了哪些菜品
            long[] dishIds = setmealDishMapper.findConnectBysetmealId(id);
            int[] statuss = dishMapper.findStatus(dishIds);
            //如果有菜品属于停售状态，就不能起售套餐
            for (int s : statuss) {
                if(s == StatusConstant.DISABLE){
                    throw new DishInSetmealException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        setmealMapper.modifyStatus(status,id);
    }

    @Override
    public int[] findStatus(long[] ids) {
        int[] statuss = setmealMapper.findStatus(ids);
        return statuss;
    }

    @Transactional
    @Override
    public void deleteSetmeal(long[] ids) {
        setmealMapper.deleteSetmeal(ids);
        //同时删除套餐菜品关系
        setmealDishMapper.deleSetmealDish(ids);
    }


    @Override
    public SetmealVO selectSetmealById(long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.findConnectionBysetmealId(id);
        //查询套餐和菜品的关系
        SetmealVO setmealVO = setmealDishMapper.findsetmealData(id);
        setmealVO.setSetmealDishes(setmealDishes);
        //查询套餐的分类名称categoryName
        String categoryName = categoryMapper.findName(setmealVO.getCategoryId());
        setmealVO.setCategoryName(categoryName);
        return setmealVO;
    }

    /*
    * 更新套餐
    * */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //重新插入菜品套餐信息,先拿到套餐id放在关系实体中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //菜品套餐关系表插入数据
        if(setmealDishes!=null && setmealDishes.size()!=0){
            setmealDishMapper.insert(setmealDishes);
        }
    }


    /**

     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**

     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
